#include "tcpsocket.hpp"
#include<signal.h>

#define CHECK_RET(r) if((r) == false){return -1;}
int main(int argc,char* argv[])
{
    if(argc!=3){
        std::cout<<"usage:"<<"./tcp_cli 172.18.0.1 9000\n";
        return -1;
    }
    signal(SIGPIPE,SIG_IGN);
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    Tcpsocket cli_sock;
    //1.创建套接字
    CHECK_RET(cli_sock.Socket());
    //2.绑定地址
    CHECK_RET(cli_sock.Connect(ip,port));
    while(1){
        //3.接受数据
        std::cout<<"client say:";
        fflush(stdout);
        std::string body;
        std::cin>>body;
        CHECK_RET(cli_sock.Send(body));
        CHECK_RET(cli_sock.Recv(&body));
        std::cout<<"serve say:"<<body<<std::endl;
    }
    //4.关闭套接字
    cli_sock.Close();
    return 0;
}